var app = getApp();
var baseUrl = app.globalData.host;
Component({
  data: {
      lands: ["请选择","无种养殖项目","有种养殖项目"],
      havenotBreedindProjFlag: 0,
      isBreeds:false,
      breeds:["请选择","自有","租赁","其他"],
      breedindProjAttrbCode1: 0,
      isbreedind:false,
      isbreedOth:false,
      landRentDelivers:["请选择","目前拖欠租金","一季一交","一年一交","一次性交付1-3年租金","一次性交付3-5年租金","一次性交付5年-10年租金","一次性交付10年以上租金","其他"],
      landRentDeliverMode:0,
      isRentDeliver:false,
      haveBusiLicences: ["请选择","是", "否"],
      haveBusiLicenceFlag: 0,
      formData: {
      },
      rules: [{
          name: 'prodOprProjName',
          rules:{required : true , message:'请输入生产经营项目'},
      }, {
          name: 'familyPopuNum',
          rules: {required: true, message: '请输入家庭成员人数'},
      }, {
          name: 'loanDtlUsageDesc',
          rules:{required:true, message: '请输入贷款用途详细说明'},
      }, {
          name: 'majorBusiOprmngAddr',
          rules: {required: true, message: '请输入主营业务经营地址'},
      }, {
          name: 'havenotBreedindProjFlag',
          rules: {required: true, message: '土地性质是必选项'},
      },{
          name: 'oprYearNum',
          rules: {required: true, message: '请输入经营年限'}, 
      },{
          name: 'haveBusiLicenceFlag',
          rules: {required: true, message: '营业执照是必选项'},
      },{
          name: 'oprmngMarea',
          rules: {required: true, message: '请输入经营面积'},
      },{
          name: 'othSituDesc',
          rules: {required: true, message: '请输入其他情况'},
      }
        ]
  },
  methods: {
    //贷款用途详细说明
    bindTextAreaBlur: function(e){
        this.setData({
            [`formData.loanDtlUsageDesc`]:e.detail.value
        }) 
    },
    //土地性质  
    bindLandNature: function(e) {
        console.log('picker country 发生选择改变，携带值为', e.detail.value);
        let isBreeds = false;
        let isbreedind = false;
        let isbreedOth = false;
        let isRentDeliver = false;
        if(e.detail.value === '2')
        {
            isBreeds = true;
        }
        else if(e.detail.value === '1')
        {
            isbreedind = false;
            isbreedOth = false;
            isRentDeliver = false;
        }
        this.setData({
        havenotBreedindProjFlag: e.detail.value,
        isBreeds:isBreeds,
        isbreedind:isbreedind,
        isbreedOth:isbreedOth,
        isRentDeliver:isRentDeliver,
        [`formData.havenotBreedindProjFlag`]: e.detail.value
        })
    },
    //有种养殖方式
    bindbreedindProjAttr: function(e){
        console.log('picker country 发生选择改变，携带值为', e.detail.value);
        let isbreedind = false;
        let isbreedOth = false;
        if(e.detail.value === '2')
        {
            isbreedind = true;
        }
        if(e.detail.value === '3')
        {
            isbreedOth = true;
        }
        this.setData({
        breedindProjAttrbCode1: e.detail.value,
        isbreedind:isbreedind,
        isbreedOth:isbreedOth,
        [`formData.breedindProjAttrbCode1`]: e.detail.value
        })
    },
    //土地租金交付方式
    bindlandRentDeliver:function(e){
        console.log('picker country 发生选择改变，携带值为', e.detail.value);
        let isRentDeliver = false;
        if(e.detail.value === '8')
        {
            isRentDeliver = true;
        }
        this.setData({
        landRentDeliverMode: e.detail.value,
        isRentDeliver:isRentDeliver,
        [`formData.landRentDeliverMode`]: e.detail.value
        })
    },
    //是否有营业执照
    bindBusinessLicense: function(e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);
    this.setData({
        haveBusiLicenceFlag: e.detail.value,
        [`formData.haveBusiLicenceFlag`]: e.detail.value
    })
    },
    //其他情况
    bindOtherAreaBlur: function(e){
        this.setData({
            [`formData.othSituDesc`]:e.detail.value
        }) 
    },
    formInputChange(e) {
        const {field} = e.currentTarget.dataset
        this.setData({
            [`formData.${field}`]: e.detail.value
        })
    },

    submitForm: function() {
        this.selectComponent('#form').validate((valid, errors) => {
            console.log('valid', valid, errors)
            if (!valid) {
                const firstError = Object.keys(errors)
                if (firstError.length) {
                    this.setData({
                        error: errors[firstError[0]].message
                    })
                }
            } else {
                var data = this.data.formData;
                console.info(data);
                this.setData({
                    disabled:true
                });
                wx.request({
                    url: baseUrl+"/report/addRptFarmSurInfoList",
                    method: 'POST',
                    data: data,
                    header: {
                        'content-type': 'application/json'
                    },
                    dataType: 'json',
                    success: function(res) { 
                        console.info(res);
                        wx.showToast({
                            title: '保存成功',
                            icon: 'none',
                            duration: 1500,
                            success:function(){
                                setTimeout(function () {
                                //要延时执行的代码
                                wx.navigateBack({ //跳转到前一个页面
                                    delta:1,//前一个页面
                                });
                                }, 1500) //延迟时间
                            }
                        });
                    },
                    fail: function(res) {
                        wx.showToast({
                        title: '服务器网络错误,请稍后重试',
                        icon: 'loading',
                        duration: 1500
                        })
                    },
                    complete: function(res) {
                    },
                })    
        }
        })
    },

    onLoad: function(option) {
    var that = this;
    console.info(option.custId);
    that.setData({
        custId:option.custId,
        [`formData.custId`]: option.custId
    });
    var data = that.data.formData;
    //查询数据回显
    wx.request({
        url: baseUrl+"/report/selectRptFarmSurInfoList",
        method: 'POST',
        data: data,
        header: {
            'content-type': 'application/json'
        },
        dataType: 'json',
        success: function(res) { 
            console.info(res);
            let date =  res.data.data;
            let havenotBreedindProjFlag = 0;
            let breedindProjAttrbCode1 = 0;
            let landRentDeliverMode = 0;
            let haveBusiLicenceFlag = 0;
            let isBreeds = false;
            let isbreedind = false;
            let isbreedOth = false;
            let isRentDeliver = false;
            if(date.landRentDeliverMode === '8')
            {
                isRentDeliver = true;
            }
            if(date.breedindProjAttrbCode1 === '2')
            {
                isbreedind = true;
            }
            if(date.breedindProjAttrbCode1 === '3')
            {
                isbreedOth = true;
            }
            if(date.havenotBreedindProjFlag === '2')
            {
                isBreeds = true;
            }
            if(date.havenotBreedindProjFlag === '1')
            {
                isbreedind = false;
                isbreedOth = false;
                isRentDeliver = false;
            }
            if(date.havenotBreedindProjFlag !== null)
            {
                havenotBreedindProjFlag = date.havenotBreedindProjFlag
            }
            if(date.breedindProjAttrbCode1 !== null)
            {
                breedindProjAttrbCode1 = date.breedindProjAttrbCode1
            }
            if(date.landRentDeliverMode !== null)
            {
                landRentDeliverMode = date.landRentDeliverMode
            }
            if(date.haveBusiLicenceFlag !== null)
            {
                haveBusiLicenceFlag = date.haveBusiLicenceFlag
            }
            that.setData({
                formData: date,
                havenotBreedindProjFlag: havenotBreedindProjFlag,
                breedindProjAttrbCode1:breedindProjAttrbCode1,
                landRentDeliverMode:landRentDeliverMode,
                haveBusiLicenceFlag:haveBusiLicenceFlag,
                isBreeds:isBreeds,
                isbreedind:isbreedind,
                isbreedOth:isbreedOth,
                isRentDeliver:isRentDeliver,
            }); 
        },
        fail: function(res) {
            wx.showToast({
            title: '服务器网络错误,请稍后重试',
            icon: 'loading',
            duration: 1500
            })
        },
        complete: function(res) {
        },
    })
    },
  }
});